package com.ghxxzx.dagl.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: 刘华
 * @Date: 2020/12/2 9:12
 * @Version 1.0
 */
public class DateUtils {
    private static String format="yyyy-MM-dd HH:mm:ss";
    private static String format1="yyyy-MM-dd";
    private static String format2="yyyy/MM/dd HH:mm:ss";

    public static Date getDateString(String date){
        SimpleDateFormat simpleDateFormat = null;
        Date s = null;
        try {
            simpleDateFormat=new SimpleDateFormat(format);
            s = simpleDateFormat.parse(date);
        } catch (Exception e) {
            try {
                simpleDateFormat=new SimpleDateFormat(format1);
                s = simpleDateFormat.parse(date);
            } catch (ParseException e1) {
                try {
                    simpleDateFormat=new SimpleDateFormat(format2);
                    s = simpleDateFormat.parse(date);
                } catch (ParseException e2) {
                    e2.printStackTrace();
                }
            }
        }
        return s;
    }
    public static Date getDateDate(Date date,Integer type){
        if (type==null){
            type=2;
        }
        String stringDate = getStringDate(date, type);
        Date date1=null;
        try {
             date1 = new SimpleDateFormat(format1).parse(stringDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date1;
    }

    public static String getStringDate(Date date,Integer integer) {
        if (integer==null){
            integer=2;
        }
        SimpleDateFormat simpleDateFormat = null;
        if (integer==1){
            simpleDateFormat = new SimpleDateFormat(format);
        }else if (integer==2){
            simpleDateFormat = new SimpleDateFormat(format1);
        }else if (integer==3){
            simpleDateFormat = new SimpleDateFormat(format2);
        }
        String s = simpleDateFormat.format(date);
        return s;
    }
}
